import time
import datetime 
import tempfile
import os.path
import cherrypy
import socket
import signal

from sqlalchemy import *
from sqlalchemy.orm import *

from platform import *
import sys

import db
from mako.template import Template
from mako.lookup import TemplateLookup

from usercontroller import usercontroller
from eventcontroller import eventcontroller
from pollcontroller import pollcontroller
from newscontroller import newscontroller
from news import news
lookup = TemplateLookup(directories='templates')

class dispatch(object):
	users = usercontroller(lookup)
	events = eventcontroller(lookup)
	polls = pollcontroller(lookup)
	news = newscontroller(lookup)


	def index(self):
		sess = db.getsession()
		currentnews = sess.query(news).all()
		return lookup.get_template("index.html").render(current = currentnews)
	index.exposed = True	

	def about(self):
		cherrypy.session['CMSUSER'] = "DAN"
		return lookup.get_template("about.html").render()
	about.exposed = True
	
	def login(self):
		sess = db.getsession()
		return lookup.get_template("login.html").render()
	login.exposed = True	

print os.path.abspath('')
cherryconfig = { 'global':	{
		'server.socket_port': 8000,
		'server.socket_host': "0.0.0.0",
		'server.thread_pool': 10,
		'tools.sessions.on': True,
		'tools.staticdir.root': os.path.abspath('')
},
	'/static':	{
		'tools.staticdir.on': True,
		'tools.staticdir.dir': "static"
		}
}

cherrypy.config.update(cherryconfig)
cherrypy.log.screen = False
cherrypy.tree.mount(dispatch(), '/', cherryconfig)
cherrypy.engine.start()
print "Web Server Started"
cherrypy.engine.block()
print "Web Server Exiting"
